#!/usr/bin/env python
#coding:utf-8
import re,sys,urllib2,json
class Get_public_ip:
    def getip(self):
        try:
            myip = self.visit("http://www.whereismyip.com/")
        except:
            try:
                myip = self.visit("http://www.ip138.com/ip2city.asp")
            except:
                myip = "So sorry!!!"
        return myip
    def visit(self,url):
        opener = urllib2.urlopen(url)
        if url == opener.geturl():
            str = opener.read()
        return re.search('\d+\.\d+\.\d+\.\d+',str).group(0)


def city():
    getmyip = Get_public_ip()
    apiurl = "http://ip.taobao.com/service/getIpInfo.php?ip=%s" % (getmyip.getip())
    content = urllib2.urlopen(apiurl).read()
    data = json.loads(content)['data']
    return  data['city'].encode("utf-8")

